package com.example.g32.park


import android.app.AlertDialog
import android.view.LayoutInflater
import androidx.activity.viewModels
import androidx.appcompat.widget.SearchView
import androidx.core.view.GravityCompat
import androidx.lifecycle.lifecycleScope
import androidx.paging.PagingData
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.base.entity.ParkEntityItem
import com.example.base.mvi.BaseActivity
import com.example.base.mvi.RequestType
import com.example.base.mvi.UiState
import com.example.g32.R
import com.example.g32.databinding.ActivityParkBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
@Route(path = "/app/ParkActivity")
class ParkActivity : BaseActivity<ActivityParkBinding, ParkIntent, ParkViewModel>() {
    override val binding by lazy {
        ActivityParkBinding.inflate(layoutInflater)
    }
    override val vm by viewModels<ParkViewModel>()
    val parkAdapter by lazy { ParkAdapter() }
    val parkAdapter2 by lazy { ParkAdapter2() }
    var type="区域"
    var para=""

    override fun initView() {
        binding.headPark.apply {
            headBack.setOnClickListener {
                finish()
            }
            headTitle.setText("车位管理")
            headAdd.setOnClickListener {
                ARouter.getInstance().build("/app/ParkAddActivity").navigation()
            }
        }

        //初始化列表
        binding.parkRv.apply {
            adapter = parkAdapter2
            layoutManager = LinearLayoutManager(this@ParkActivity)
        }
        //长按删除
//        parkAdapter.setOnItemLongClickListener(object :OnItemLongClickListener{
//            override fun onitemLongClick(position: Int): Boolean {
//                //发送删除请求
//                vm.sendIntent(ParkIntent.deletePark(parkAdapter.snapshot().get(position)?.id!!))
//                return true
//            }
//        })

        parkAdapter2.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/ParkEditActivity").withString("pid",parkAdapter2.data.get(position).id.toString()).navigation()
        }

        //点击跳转至VR
//        parkAdapter.setOnItemClickListener(object :OnItemClickListener{
//            override fun onItemClick(position: Int, item: ParkEntityItem) {
//                ARouter.getInstance().build("/app/ParkVRActivity").navigation()
//            }
//
//        })

        //进入VR
//       parkAdapter.setOnItemClickListener(object :OnItemClickListener{
//           override fun onItemClick(position: Int) {
//               ARouter.getInstance().build("/app/ParkVRActivity").navigation()
//           }
//
//       })

        //左滑删除
        val touch = ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(ItemTouchHelper.LEFT, ItemTouchHelper.LEFT) {
            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder
            ): Boolean {
                return false
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                val position = viewHolder.adapterPosition
                vm.sendIntent(ParkIntent.deletePark(parkAdapter2.data.get(position)?.id!!))
            }

        })
         touch.attachToRecyclerView(binding.parkRv)

        //点击筛选
        binding.parkFilter.setOnClickListener {
            binding.parkDrawer.openDrawer(GravityCompat.END)
        }

        binding.parkSv.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                para = query!!
                if (type == "区域") {
                    vm.sendIntent(ParkIntent.getParkByArea(query!!))
                } else {
                    vm.sendIntent(ParkIntent.getParkByCarNo(query!!))
                }

                //根据车牌号查找
               // vm.sendIntent(ParkIntent.queryById(query.toInt()))
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                if(newText.isNullOrEmpty()){
                    vm.sendIntent(ParkIntent.getParkByType("标准车位"))
                }
                return true
            }
        })

        binding.parkRgstatus.setOnCheckedChangeListener { group, checkedId ->
            var status=""
            when(checkedId){
                R.id.rb_statuskx->{
                    status="空闲"
                    binding.rbStatuskx.isChecked=false
                }
                R.id.rb_stauszy->{
                    status="已占用"
                    binding.rbStauszy.isChecked=false
                }
                R.id.rb_stauswh->{
                    status="维护中"
                    binding.rbStauswh.isChecked=false
                }
            }
            vm.sendIntent(ParkIntent.getParkByStatus(status))
            binding.parkDrawer.closeDrawer(GravityCompat.END)
        }

        binding.parkRgtype.setOnCheckedChangeListener { group, checkedId ->
            var type=""
            when(checkedId){
                R.id.rb_typea->{
                    type="标准车位"
                    binding.rbTypea.isChecked=false
                }
                R.id.rb_typeb->{
                    type="VIP车位"
                    binding.rbTypeb.isChecked=false
                }
            }
            vm.sendIntent(ParkIntent.getParkByType(type))
            binding.parkDrawer.closeDrawer(GravityCompat.END)
        }

        binding.parkCarNo.setOnClickListener {
            type="车牌号"
            vm.sendIntent(ParkIntent.getParkByCarNo(para))
            binding.parkDrawer.closeDrawer(GravityCompat.END)
        }
    }


    override fun initData() {
        lifecycleScope.launch {
//            lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
            vm.uiState.collect {
                when (it) {
                    is UiState.onSuccess<*> -> {
                        when(it.type){
                            RequestType.DELETE->{
                                ToastUtils.showLong("删除成功")
                                vm.sendIntent(ParkIntent.getParkByType("标准车位"))
                            }
                            RequestType.DEFAULT->{
                                parkAdapter2.data.clear()
                                val items = it.data as Collection<ParkEntityItem>
                                parkAdapter2.addData(items)
//                                    val data = it.data as PagingData<ParkBeanItem>
//                                    parkAdapter.submitData(lifecycle,data)
                            }
                            RequestType.PARKITEM->{
                                parkAdapter2.data.clear()
                                val items = it.data as ParkEntityItem
                                parkAdapter2.addData(items)
                            }

                            else -> {}
                        }
                    }
                    else -> {}
                }
            }
        }
    }
    override fun onResume() {
        super.onResume()
        vm.sendIntent(ParkIntent.getParkByType("标准车位"))
    }
}

